package com.leyu.mall.admin.goods.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leyu.mall.common.facade.ICmsChannelFacade;
import com.leyu.mall.common.model.GoodsCategoryModel;
import com.leyu.mall.domain.cms.CmsChannel;
import com.leyu.mall.domain.goods.GoodsCategory;
import com.leyu.mall.service.cms.ICmsChannelService;
import com.leyu.mall.service.goods.IGoodsCategoryService;

/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * 
 * @author OOKK
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("admin/category")
public class GoodsCategoryController {

	@Resource(name = "goodsCategoryServiceImpl")
	private IGoodsCategoryService goodsCategoryServiceImpl;
	@Autowired
	private ICmsChannelService	cmsChannelService;
	@Autowired
	private ICmsChannelFacade cmsChannelFacade;
	

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Model model, GoodsCategory goodsCategory) {
		try {
			//解析网页传送过来的getParentName, <option value="${data.categoryID}||${data.categoryName}" >
			//parentID解析出来set到model中；
			//System.out.println("---------------goodsCategory+"+goodsCategory);
			String parentstr [] = goodsCategory.getParentName().split("=");
			//Long parentID = Long.parseLong(parentidstr[0]);
			goodsCategory.setParentID(Long.parseLong(parentstr[0]));
			goodsCategory.setParentName(parentstr[1]);
			
			goodsCategoryServiceImpl.save(goodsCategory);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../category";
	}

	@RequestMapping(value = "delete/{categoryID}", method = RequestMethod.GET)
	public String delete(Model model, @PathVariable long categoryID) {
		try {
			goodsCategoryServiceImpl.deleteGoodsCategory(categoryID);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../../category";
	}

	@RequestMapping(value = "edit/{categoryID}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable long categoryID) {
		try {
			model.addAttribute("channellist", cmsChannelService.getcmsChannelList());
			model.addAttribute("levellist", goodsCategoryServiceImpl.getAllCategoryLevel());
			model.addAttribute("entity", goodsCategoryServiceImpl
					.getGoodsCategoryById(categoryID));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/editcategory.html";
	}

	@ExceptionHandler(Exception.class)
	public String exception(Exception e, HttpServletRequest request) {
		request.setAttribute("exception", e);
		return "/error.html";
	}

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String newCategory(Model model) throws Exception {
		System.out.println("---------------");
		model.addAttribute("levellist", goodsCategoryServiceImpl.getAllCategoryLevel());
		model.addAttribute("channellist", cmsChannelFacade.getcmsChannelList());
		return "admin/newcategory.html";
	}

	
	@RequestMapping(value = "categorylist", method = RequestMethod.GET)
	public @ResponseBody List categoryList(Model model) throws Exception {
		List<GoodsCategory> categoryList = goodsCategoryServiceImpl.getAllGoodsCategory();
		model.addAttribute("categorylist", categoryList);
		return categoryList;
	}
	
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String index(Model model) throws Exception {
		try {
			List<GoodsCategoryModel> modelList = new ArrayList();
			List<GoodsCategory> cmscategoryList = goodsCategoryServiceImpl.getAllGoodsCategory();
			List<CmsChannel> cmschannelList = cmsChannelService.getcmsChannelList();
			for (GoodsCategory cmscategory : cmscategoryList)
			{
				for (CmsChannel cmschannel : cmschannelList)
				{
					if (cmscategory.getChannelID() == cmschannel.getChannelId())
					{
						GoodsCategoryModel cm = new GoodsCategoryModel();
						BeanUtils.copyProperties(cmscategory, cm);
						cm.setChannelTitle(cmschannel.getChannelTitle());
						modelList.add(cm);
					}
				}
			}
			model.addAttribute("list", modelList);
			
//		model.addAttribute("list", goodsCategoryServiceImpl.getAllGoodsCategory());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/category.html";
	}
}
